home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MetalRadioButtonUI.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  155 lines

  1. /*
  2.  * @(#)MetalRadioButtonUI.java    1.5 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.metal;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import com.sun.java.swing.*;
  26. import com.sun.java.swing.plaf.basic.*;
  27. import com.sun.java.swing.border.*;
  28. import com.sun.java.swing.plaf.*;
  29. import java.io.Serializable;
  30.  
  31.  
  32. /**
  33.  * RadioButtonUI implementation for MetalRadioButtonUI
  34.  * <p>
  35.  * Warning: serialized objects of this class will not be compatible with
  36.  * future swing releases.  The current serialization support is appropriate
  37.  * for short term storage or RMI between Swing1.0 applications.  It will
  38.  * not be possible to load serialized Swing1.0 objects with future releases
  39.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  40.  * baseline for the serialized form of Swing objects.
  41.  *
  42.  * @version 1.5 02/02/98
  43.  * @author Michael C. Albers
  44.  */
  45. public class MetalRadioButtonUI extends BasicRadioButtonUI {
  46.  
  47.     private static final MetalRadioButtonUI metalRadioButtonUI = new MetalRadioButtonUI();
  48.  
  49.     public static ComponentUI createUI(JComponent c) {
  50.         return metalRadioButtonUI;
  51.     }
  52.  
  53.  
  54.     /************************** The View *************************/
  55.     protected ButtonModel model;
  56.     Font f;
  57.     FontMetrics fm;
  58.     Rectangle viewRect, iconRect, textRect;
  59.     Icon altIcon, selectedIcon, disabledIcon;
  60.  
  61.     /**
  62.      * paint the radio button
  63.      */
  64.     public synchronized void paint(Graphics g, JComponent c) {
  65.  
  66.     AbstractButton b = (AbstractButton) c;
  67.     model = b.getModel();
  68.     
  69.     Dimension size = c.getSize();
  70.  
  71.     int w = size.width;
  72.     int h = size.height;
  73.  
  74.     f = c.getFont();
  75.     g.setFont(f);
  76.     fm = g.getFontMetrics();
  77.  
  78.     viewRect = new Rectangle(size);
  79.         iconRect = new Rectangle();
  80.         textRect = new Rectangle();
  81.  
  82.     altIcon = b.getIcon();
  83.     selectedIcon = null;
  84.     disabledIcon = null;
  85.     
  86.         String text = SwingUtilities.layoutCompoundLabel(
  87.             fm, b.getText(), altIcon != null ? altIcon : icon,
  88.             b.getVerticalAlignment(), b.getHorizontalAlignment(),
  89.             b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
  90.             viewRect, iconRect, textRect, getDefaultTextIconGap(b)
  91.         );
  92.     
  93.         // fill background
  94.     if(c.isOpaque()) {
  95.         g.setColor(b.getBackground());
  96.         g.fillRect(0,0, size.width, size.height); 
  97.     }
  98.  
  99.     
  100.     // Paint the radio button
  101.         if(altIcon != null) { 
  102.  
  103.             if(!model.isEnabled()) {
  104.                 altIcon = b.getDisabledIcon();
  105.             } else if(model.isPressed() && model.isArmed()) {
  106.                 altIcon = b.getPressedIcon();
  107.                 if(altIcon == null) {
  108.                     // Use selected icon
  109.                     altIcon = b.getSelectedIcon();
  110.                 } 
  111.             } else if(model.isSelected()) {
  112.                 altIcon = b.getSelectedIcon();
  113.         } else if(b.isRolloverEnabled() && model.isRollover()) {
  114.                 altIcon = (Icon) b.getRolloverIcon();
  115.             } 
  116.           
  117.         if(altIcon == null) {
  118.         altIcon = b.getIcon();
  119.         }
  120.            
  121.         altIcon.paintIcon(c, g, iconRect.x, iconRect.y);
  122.  
  123.         } else {
  124.         icon.paintIcon(c, g, iconRect.x, iconRect.y);
  125.     }
  126.  
  127.  
  128.     // Draw the Text
  129.     if(text != null) {
  130.         if(model.isEnabled()) {
  131.         // *** paint the text normally
  132.         g.setColor(b.getForeground());
  133.                 BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  134.                                               textRect.x,
  135.                           textRect.y + fm.getAscent());
  136.         } else {
  137.         // *** paint the text disabled
  138.         g.setColor(b.getBackground().darker());
  139.                 BasicGraphicsUtils.drawString(g,text,model.getMnemonic(),
  140.                                               textRect.x,
  141.                           textRect.y + fm.getAscent());
  142.         }
  143.             if(b.hasFocus() && b.isFocusPainted() &&
  144.            textRect.width > 0 && textRect.height > 0 ) {
  145.         paintFocus(g,textRect,size);
  146.             }
  147.     }
  148.     }
  149.  
  150.     protected void paintFocus(Graphics g, Rectangle t, Dimension d){
  151.         g.setColor(UIManager.getColor("RadioButton.focus"));
  152.         g.drawRect(t.x,t.y-1,t.width+1,t.height+1);
  153.     } 
  154. }
  155.